<?php

class Programs extends HTech_Db_Table implements Emms_Interface_Performance
{
    protected $_name    = 'view_programs';
    protected $_primary = 'ProgramId'; 
    
    protected $_dependentTables = array('LnkLocationsPrograms','LnkProgramsFunds');
    protected $_lnkTables = array('LnkProgramsFunds' => 'FundId', 'LnkLocationsPrograms' => 'LocationId');

    protected $_referenceMap    = array(
        'ProductLines' => array(
            'columns'           => array('ProductLineId'),
            'refTableClass'     => 'ProductLines',
            'refColumns'        => array('ProductLineId')
        )
    );  
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Programs");
        $this->_gridMetadata['onClick'] = 'admin.program.view';
        $this->_gridMetadata['headers'] = array
            (
                array('ProgramId',          50, $this->translate("Id"),             'Number'),
                array('Program',           200, $this->translate("Program"),        'String'),
                array('Status',            100, $this->translate("Status"),         'String'),
                array('Business',          100, $this->translate("Business"),       'String'),
                array('PublicSponsorship', 100, $this->translate("Public"),         'String'),
                array('ProductLine',       200, $this->translate("Line"),           'String'),
                array('SurveyName',        300, $this->translate("Survey"),         'String'),
                array('Comments',          500, $this->translate("Comments"),       'String')
            );
        return $this;
    }
    
    protected function _setRecordDependentRows()
    {
        $this->_record->r = (object) array('funds' => null, 'locations' => null);
        $this->_record->r->funds = $this->_record->d->findManyToManyRowset('Funds','LnkProgramsFunds');
        array_walk($this->_record->r->funds->toArray(), array($this, '_setRecordLnkData'), array('LnkProgramsFunds', 'FundId'));
        
        $this->_record->r->locations = $this->_record->d->findManyToManyRowset('Locations','LnkLocationsPrograms'); 
        array_walk($this->_record->r->locations->toArray(), array($this, '_setRecordLnkData'), array('LnkLocationsPrograms', 'LocationId'));
    }
    
    protected function _setRecordPerformance() 
    {
        $this->_record->p = $this->getPerformance($this->_record->d, new Zend_Date());
    }
       
    static function getProgramOptions($type = '', $id = 0)
    {
        if(!$id)            
            return array('' => '--- ' . self::translate('Select program')) + 
                Zend_Registry::get('db')->fetchPairs("select ProgramId, Program 
                    from programs where Status = ? order by Program", 'Active');
        
        switch($type) {
            case 'Line': 
                return Zend_Registry::get('db')->fetchPairs("select ProgramId, Program from programs 
                    where ProductLineId = ? order by Program", $id);
        }
        return array();
    }    
        
    public function getPerformanceCharts(Zend_Db_Table_Row $row)
    {   
        return array(
            'title' => $row['Program'],
            'subtitle' => self::translate('Performance charts'),
            'params' => array(
                'p1' => 'ProgramId',
                'v1' => $row['ProgramId'],
                'p2' => 'Program',
                'v2' => urlencode($row['Program'])),
            'charts' => array(
                'day' => array(
                    'chartId' => 'ProgramPerformanceDay',
                    'chartData' => '/admin/chart/performance/Mode/Day/Subject/Program/id/' . $row['ProgramId'],
                    'chartSizeX' => 350,
                    'chartSizeY' => 320),
                'week' => array(
                    'chartId' => 'ProgramPerformanceWeek',
                    'chartData' => '/admin/chart/performance/Mode/Week/Subject/Program/id/' . $row['ProgramId'],
                    'chartSizeX' => 200,
                    'chartSizeY' => 320),
                'month' => array(
                    'chartId' => 'ProgramPerformanceMonth',
                    'chartData' => '/admin/chart/performance/Mode/Month/Subject/Program/id/' . $row['ProgramId'],
                    'chartSizeX' => 600,
                    'chartSizeY' => 320)
            )
        );
    }
    
    public function getPerformance(Zend_Db_Table_Row $row, Zend_Date $date, $options = array())
    {
        $row = $row; $date = $date; $options = $options;
        
        $ps = array();
        $pc = array();
        
        $pf = Zend_Registry::get('db')->query("select * from performance_data where Date = curdate() and ProgramId = ?", 
                array($row['ProgramId']))->fetchAll();
        
        foreach($pf as $metric) {
            $ps[$metric->Metric] = $metric->Value;
            $pc[$metric->Metric] = $metric->Color;
        }
        $ps['c'] = (object) $pc;
        return (object) $ps;
    }
}